﻿Public Class ThisAddIn

    Protected Overrides Function CreateRibbonExtensibilityObject() As Microsoft.Office.Core.IRibbonExtensibility
        Return New Ribbon1()
    End Function

    Private Sub ThisAddIn_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup
        CreateRibbonExtensibilityObject()
    End Sub

    Private Sub ThisAddIn_Shutdown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shutdown

    End Sub

    Private Sub Application_DocumentBeforeSave(ByVal Doc As Word.Document, ByRef SaveAsUI As Boolean, _
    ByRef Cancel As Boolean) Handles Application.DocumentBeforeSave

        'Dim objWriter As New System.IO.StreamWriter("D:\documentTest.txt")

        Dim numOfParagraphs As Integer = Doc.Paragraphs.Count()
        Dim myDocument As New MyDocument(numOfParagraphs)
        Dim myParagraph As MyParagraph
        Dim mySentence As MySentence
        Dim paragraphIndex As Integer = 0

        For Each paragraph As Word.Paragraph In Doc.Paragraphs

            myDocument.paragraphs(paragraphIndex) = New MyParagraph(paragraph.Range.Sentences.Count)
            myParagraph = myDocument.paragraphs(paragraphIndex)
            Dim sentenceIndex As Integer = 0

            'Dim inlineShapes As Word.InlineShapes = Doc.InlineShapes
            'For Each picture As Word.InlineShape In inlineShapes
            'Dim type As Word.WdInlineShapeType = picture.Type
            'Dim name As String = picture.Title
            'Next

            ' Hier werden Bilder gefunden, die "Zeilenumbruch->passend" haben
            Dim shapes As Word.Shapes = Doc.Shapes
            For Each shape As Word.Shape In shapes
                Dim name As String = shape.Name
                Dim title As String = shape.Title
            Next

            For Each sentence As Word.Range In paragraph.Range.Sentences

                myParagraph.sentences(sentenceIndex) = New MySentence(sentence.Words.Count)
                mySentence = myParagraph.sentences(sentenceIndex)
                Dim formats As New Dictionary(Of MyFormat, Integer)

                ' Formate: 
                'Es wird das für diesen Satz vorherrschende Format gesucht
                For Each word As Word.Range In sentence.Words

                    Dim found As Boolean = False
                    Dim pairValue = 1
                    Dim highestValue = 0
                    Dim newFormat As New MyFormat(word.Font.Name, word.Font.Size, word.Font.Bold, word.Font.Italic, (word.Font.Underline > 0))

                    For Each pair In formats
                        If newFormat.isEqual(pair.Key) Then
                            pairValue = pair.Value + 1
                            formats.Remove(pair.Key)
                            found = True
                            Exit For
                        End If
                    Next

                    formats.Add(newFormat, pairValue)
                    If highestValue < pairValue Then
                        mySentence.commonFormat = newFormat
                        highestValue = pairValue
                    End If

                Next

                Dim wordIndex As Integer = 0

                For Each word As Word.Range In sentence.Words

                    If (AscW(word.Text.Substring(0, 1))) > 32 Then
                        mySentence.words(wordIndex) = word.Text
                        wordIndex += 1
                    ElseIf AscW(word.Text.Substring(0, 1)) = 9 Then 'Tabulator
                        mySentence.words(wordIndex) = ""
                        mySentence.specialChars.Add(wordIndex, "<TAB>")
                        wordIndex += 1
                    End If

                    Dim newFormat As New MyFormat(word)
                    If Not (newFormat.isEqual(mySentence.commonFormat)) Then
                        mySentence.specialFormat.Add(wordIndex - 1, newFormat)
                    End If

                Next

                mySentence.cleanArray()
                sentenceIndex += 1
            Next

            paragraphIndex += 1
        Next

        'objWriter.Close()
        'objWriter.Dispose()

    End Sub

End Class
